﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
//using System.Text;
using System.IO;

namespace Fabrikator
{
    public class Yardimci
    {
        public Yardimci() { }
        public string KodTemizle(string kaynakca)
        {
            string sonuc;
            sonuc = kaynakca.Replace("\r", " ");
            sonuc = sonuc.Replace("<a", "");
            sonuc = sonuc.Replace("<i", "");
            sonuc = sonuc.Replace("<b", "");
            sonuc = sonuc.Replace("<s", "");
            sonuc = sonuc.Replace("<d", "");
            sonuc = sonuc.Replace("/<", "");
            sonuc = sonuc.Replace("/>", "");
            sonuc = sonuc.Replace("\n", " ");
            sonuc = sonuc.Replace("\t", string.Empty);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"( )+", " ");
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"<( )*head([^>])*>", "<head>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"(<( )*(/)( )*head( )*>)", "</head>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, "(<head>).*(</head>)", string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"<( )*script([^>])*>", "<script>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"(<( )*(/)( )*script( )*>)", "</script>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"(<script>).*(</script>)", string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"<( )*style([^>])*>", "<style>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"(<( )*(/)( )*style( )*>)", "</style>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, "(<style>).*(</style>)", string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"<( )*td([^>])*>", "\t", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"<( )*br( )*>", "\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"<( )*li( )*>", "\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"<( )*div([^>])*>", "\r\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"<( )*tr([^>])*>", "\r\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"<( )*p([^>])*>", "\r\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"<[^>]*>", string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @" ", " ", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"&bull;", " * ", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"&lsaquo;", "<", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"&rsaquo;", ">", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"&trade;", "(tm)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"&frasl;", "/", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"&lt;", "<", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"&gt;", ">", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"&copy;", "(c)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"&reg;", "(r)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, @"&(.{2,6});", string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = sonuc.Replace("\n", "\r");
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, "(\r)( )+(\r)", "\r\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, "(\t)( )+(\t)", "\t\t", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, "(\t)( )+(\r)", "\t\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, "(\r)( )+(\t)", "\r\t", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, "(\r)(\t)+(\r)", "\r\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            sonuc = System.Text.RegularExpressions.Regex.Replace(sonuc, "(\r)(\t)+", "\r\t", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            string breaks = "\r\r\r";
            string tabs = "\t\t\t\t\t";
            for (int index = 0; index < sonuc.Length; index++)
            {
                sonuc = sonuc.Replace(breaks, "\r\r");
                sonuc = sonuc.Replace(tabs, "\t\t\t\t");
                breaks = breaks + "\r";
                tabs = tabs + "\t";
            }
            return sonuc;
        }
        private const string passPhrase = "fabrikator";


        // herhangi birşey olabilir
        private const string saltValue = "testediliyor";


        // SHA1 ya da MD5
        private const string hashAlgorithm = "SHA1";


        // herhangi bir sayı olabilir
        private const int passwordIterations = 2;


        // 16 byte olmalı
        private const string initVector = "@1B2c3Dq@5F6x7H8";


        // kaç bit şifreleme?
        private const int keySize = 256;

        public string Sifrele(string plainText, string Anahtar)
        {
            byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
            byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);

            byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

            PasswordDeriveBytes password = new PasswordDeriveBytes(Anahtar, saltValueBytes, hashAlgorithm, passwordIterations);

            byte[] keyBytes = password.GetBytes(keySize / 8);

            RijndaelManaged symmetricKey = new RijndaelManaged();

            symmetricKey.Mode = CipherMode.CBC;

            ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);

            MemoryStream memoryStream = new MemoryStream();

            CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);

            cryptoStream.FlushFinalBlock();

            byte[] cipherTextBytes = memoryStream.ToArray();

            memoryStream.Close();
            cryptoStream.Close();

            string cipherText = Convert.ToBase64String(cipherTextBytes);

            return cipherText;
        }

        public string SifreCoz(string cipherText, string Anahtar)
        {
            byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
            byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);

            byte[] cipherTextBytes = Convert.FromBase64String(cipherText);

            PasswordDeriveBytes password = new PasswordDeriveBytes(Anahtar, saltValueBytes, hashAlgorithm, passwordIterations);

            byte[] keyBytes = password.GetBytes(keySize / 8);

            RijndaelManaged symmetricKey = new RijndaelManaged();

            symmetricKey.Mode = CipherMode.CBC;

            ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);

            MemoryStream memoryStream = new MemoryStream(cipherTextBytes);

            CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);

            byte[] plainTextBytes = new byte[cipherTextBytes.Length];

            int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);

            memoryStream.Close();
            cryptoStream.Close();

            string plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);

            return plainText;
        }
    }
}